#pragma once
#include <vector>
#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <queue>
#include <string>
#include "Task.hpp"

struct ThreadInfo
{
    pthread_t tid;
    std::string name;
};

template <class T>
class ThreadPool
{
    static const int defaultnum = 5;

private:
    void Lock()
    {
        pthread_mutex_lock(&mutex_);
    }
    void Unlock()
    {
        pthread_mutex_unlock(&mutex_);
    }
    void Wakeup()
    {
        pthread_cond_signal(&cond_);
    }
    void Wait()
    {
        pthread_cond_wait(&cond_, &mutex_);
    }
    bool IsQueueEmpty()
    {
        return tasks_.empty();
    }
    std::string GetThreadName(pthread_t tid)
    {
        for (int i = 0; i < threads_.size(); ++i)
        {
            if (pthread_equal(threads_[i].tid, tid))
            {
                return threads_[i].name;
            }
        }
        return "unknown";
    }
    T pop()
    {
        T task = tasks_.front();
        tasks_.pop();
        return task;
    }

public:
    static void *HandlerTask(void *arg)
    {
        ThreadPool<T> *tp = static_cast<ThreadPool<T> *>(arg);
        std::string threadname = tp->GetThreadName(pthread_self());
        while (true)
        {
            tp->Lock();
            while (tp->IsQueueEmpty())
            {
                tp->Wait();
            }
            T task = tp->pop();
            tp->Unlock();
            task.run();
            std::cout << threadname << " result: " << task.GetResult() << std::endl;
        }
    }

    void Start()
    {
        for (int i = 0; i < threads_.size(); ++i)
        {
            pthread_create(&(threads_[i].tid), nullptr, HandlerTask, this);
            threads_[i].name = "thread-" + std::to_string(i);
        }
    }

    void Push(const T &task)
    {
        Lock();
        tasks_.push(task);
        Unlock();
        Wakeup();
    }
    static ThreadPool<T> *GetThreadPool()
    {
        if (tp_ == nullptr)
        {
            pthread_mutex_lock(&lock_);
            if (tp_ == nullptr)
            {
                std::cout << "log: singleton threadpool create!" << std::endl;
                tp_ = new ThreadPool<T>();
            }
            pthread_mutex_unlock(&lock_);
        }
        return tp_;
    }

private:
    ThreadPool(int num = defaultnum)
        : threads_(num)
    {
        pthread_mutex_init(&mutex_, nullptr);
        pthread_cond_init(&cond_, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&mutex_);
        pthread_cond_destroy(&cond_);
    }
    ThreadPool(const ThreadPool<T> &) = delete;
    ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;

private:
    std::vector<ThreadInfo> threads_;
    std::queue<T> tasks_;
    pthread_mutex_t mutex_;
    pthread_cond_t cond_;

    static ThreadPool<T> *tp_;
    static pthread_mutex_t lock_;
};

template <class T>
ThreadPool<T> *ThreadPool<T>::tp_ = nullptr;

template <class T>
pthread_mutex_t ThreadPool<T>::lock_ = PTHREAD_MUTEX_INITIALIZER;